package com.zhm.demo.redis;

import com.hthium.json.util.ValueWrapper;
import com.zhm.demo.redis.config.ValueWrapperRedisSerializer;
import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

public class ValueWrapperRedisTemplate extends RedisTemplate<String, ValueWrapper> {

    /**
     * Constructs a new <code>StringRedisTemplate</code> instance. {@link #setConnectionFactory(RedisConnectionFactory)}
     * and {@link #afterPropertiesSet()} still need to be called.
     */
    public ValueWrapperRedisTemplate() {
        RedisSerializer<ValueWrapper> valueWrapperRedisSerializer = new ValueWrapperRedisSerializer();
        setHashKeySerializer(RedisSerializer.string()); // Hash value序列化
        setHashValueSerializer(valueWrapperRedisSerializer);
        setKeySerializer(RedisSerializer.string());
        setValueSerializer(valueWrapperRedisSerializer);
    }

    /**
     * Constructs a new <code>StringRedisTemplate</code> instance ready to be used.
     *
     * @param connectionFactory connection factory for creating new connections
     */
    public ValueWrapperRedisTemplate(RedisConnectionFactory connectionFactory) {
        this();
        setConnectionFactory(connectionFactory);
        afterPropertiesSet();
    }

    protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
        return new DefaultStringRedisConnection(connection);
    }
}